import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study/changenotifier/test_consumer_notifier.dart';
import 'package:flutter_study/changenotifier/test_selector_notifier.dart';
import 'package:flutter_study/utils/Utils.dart';
import 'package:provider/provider.dart';

class InheritedWidgetStudyPage extends StatefulWidget {
  const InheritedWidgetStudyPage({Key? key}) : super(key: key);

  @override
  _InheritedWidgetStudyPageState createState() =>
      _InheritedWidgetStudyPageState();

  static _ShareData? of(BuildContext context, String aspect) {
    return InheritedModel.inheritFrom<_ShareData>(context, aspect: aspect);
  }
}

class _InheritedWidgetStudyPageState extends State<InheritedWidgetStudyPage> {
  final String name = "";
  final String age = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("provider"),
      ),
      body: Container(
        child: Column(
          children: [
            InkWell(
              onTap: () {
                context.read<TestConsumerNotifier>().getFuture("start", "end");
              },
              child: Consumer<TestConsumerNotifier>(
                builder: (context, provider, child) {
                  loge(provider.message);
                  return Text(provider.message);
                },
              ),
            ),
            InkWell(
              onTap: () {
                context.read<TestSelectorNotifier>().updateAge("更改名字李白");
              },
              child: Selector<TestSelectorNotifier,String>(
                selector: (_,notifier) => notifier.age,
                builder: (context, value, child) {
                  loge(value);
                  return Text(value);
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

class _ShareData extends InheritedModel<String> {
  final String shareOne;
  final String shareTwo;

  _ShareData(this.shareOne, this.shareTwo, Widget child) : super(child: child);

  @override
  bool updateShouldNotify(covariant _ShareData oldWidget) {
    return oldWidget.shareOne != shareOne || oldWidget.shareTwo != shareTwo;
  }

  @override
  bool updateShouldNotifyDependent(
      covariant _ShareData oldWidget, Set<String> dependencies) {
    // TODO: implement updateShouldNotifyDependent
    if (dependencies.contains("one") && oldWidget.shareOne != shareOne) {
      return true;
    }
    if (dependencies.contains("two") && oldWidget.shareTwo != shareTwo) {
      return true;
    }
    return false;
  }
}
